#[macro_use] extern crate rust_admin2_common;
//#[macro_use] extern crate anyhow;

use actix_web::{App, HttpServer, middleware};
use sqlx::postgres::PgPool;
use futures::executor;

#[macro_use]
mod common;
mod apps;

/// 控制器的文件
use apps::backend::controllers::{
    Index, Banks, Provinces, Cities,
    Users, UserAccounts, UserDetails, UserCards,
    Menus, AdminRoles, Admins, AdminLogs,
    Recharges, Withdraws, AccountChanges,
};

// ------------------------ 启动端口、地址 -----------------------/ 
/// 绑定主机地址
const HOST: &'static str = "127.0.0.1";
/// 绑定端口号码
const PORT: &'static str = "8092";
// ------------------------ 数据库的信息配置 -----------------------/ 
/// 数据库名
const DB_NAME: &'static str = "rust_admin_2";
/// 主机地址
const DB_HOST: &'static str = "localhost";
/// 用户名称
const DB_USER: &'static str =  "postgres";
/// 用户密码
const DB_PASS: &'static str = "";
// ------------------------ 数据库的信息配置 -----------------------/ 

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    
    // 正式环境可以去掉日志显示
    std::env::set_var("RUST_LOG", "actix_web=info");
    env_logger::init(); //正式环境可以注释此行
    let host_port = &format!("{}:{}", HOST, PORT);

    println!("启动程序: http://{}", host_port);
    
    // 数据连接信息: postgres://用户:密码@主机/数据库名
    //let conn_str = "postgres://postgres:@localhost/rust_admin_2";
    let conn_str = &format!("postgres://{}:{}@{}/{}", DB_USER, DB_PASS, DB_HOST, DB_NAME);
    let pool = dbg!(executor::block_on(PgPool::new(&conn_str)).unwrap());

    HttpServer::new(move || {
        App::new()
            .wrap(middleware::Logger::default()) //正式环境可以注释此行 ***
            .data(pool.clone())
            .service(get!("/api/v1/test", Index::test))
            .service(post!("/v1/login", Index::login))

            .service(post!("/v1/banks", Banks::index)) // 银行列表
            .service(post!("/v1/provinces", Provinces::index)) // 省份列表
            .service(post!("/v1/cities", Cities::index)) // 城市列表

            .service(post!("/v1/users", Users::index)) // 用户相关
            .service(post!("/v1/user_accounts", UserAccounts::index)) // 用户账户相关
            .service(post!("/v1/user_details", UserDetails::index)) // 用户信息
            .service(post!("/v1/user_cards", UserCards::index)) // 用户绑卡

            .service(post!("/v1/recharges", Recharges::index)) // 用户绑卡
            .service(post!("/v1/withdraws", Withdraws::index)) // 用户绑卡
            .service(post!("/v1/account_changes", AccountChanges::index)) // 用户绑卡

            .service(post!("/v1/menus", Menus::index)) // 用户绑卡
            .service(post!("/v1/admin_roles", AdminRoles::index)) // 用户绑卡
            .service(post!("/v1/admin_logs", AdminLogs::index)) // 用户绑卡
            .service(post!("/v1/admins", Admins::index)) // 用户绑卡
    })
    .bind(host_port)?
    .run()
    .await
}
